﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class STCamDeviceController : MonoBehaviour
{

	WebCamTexture camTexture;
	CanvasScaler CanScaler;
	Camera ca;
	Image img;
	RawImage ShadowImg;

	public GameObject CamRoot = null;
	public GameObject CharacterRoot = null;

	void Awake () {

		Input.gyro.enabled = true;

		Vector3 deviceGravity = Input.gyro.gravity;
		Vector3 rotationVelocity = Input.gyro.rotationRate;
		Vector3 rotationVelocity2 = Input.gyro.rotationRateUnbiased;
		Input.gyro.updateInterval = 0.016f;
		Vector3 acceleration = Input.gyro.userAcceleration;


		img = GetComponentInChildren<Image>();
		//ShadowImg = GetComponentInChildren<RawImage>();

		CanScaler = GetComponentInChildren<CanvasScaler> ();
		CanScaler.referenceResolution = new Vector2 (Screen.width, Screen.height);

		ca = GetComponentInChildren<Camera> ();
		ca.orthographicSize = Screen.width / 100.0f/ 2.0f;

		img.transform.localScale = new Vector3 (-1, -1, -1);

		img.rectTransform.anchorMin = new Vector2 (0.5f, 0.5f);
		img.rectTransform.anchorMax = new Vector2 (0.5f, 0.5f);
		img.rectTransform.pivot = new Vector2(0.5f,0.5f);

		img.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, Screen.height);
		img.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, Screen.width);

//		ShadowImg.transform.localScale = new Vector3 (-1, -1, -1);
//
//		ShadowImg.rectTransform.anchorMin = new Vector2 (0.5f, 0.5f);
//		ShadowImg.rectTransform.anchorMax = new Vector2 (0.5f, 0.5f);
//		ShadowImg.rectTransform.pivot = new Vector2(0.5f,0.5f);
//
//		ShadowImg.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, Screen.height);
//		ShadowImg.rectTransform.SetSizeWithCurrentAnchors (RectTransform.Axis.Vertical, Screen.width);

		// 设备不同的坐标转换
		#if UNITY_IOS || UNITY_IPHONE
		img.transform.Rotate (new Vector3 (0, 180, 90));
		//ShadowImg.transform.Rotate (new Vector3 (0, 180, 90));
		#elif UNITY_ANDROID
		img.transform.Rotate (new Vector3 (0, 0, 90));
		//ShadowImg.transform.Rotate (new Vector3 (0, 0, 90));
		#endif



	}

	void Start()
	{
		

		StartCoroutine(CallCamera());
	}

	IEnumerator CallCamera()
	{
		yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
		if (Application.HasUserAuthorization(UserAuthorization.WebCam))
		{
			if (camTexture != null)
				camTexture.Stop();

			WebCamDevice[] cameraDevices = WebCamTexture.devices;

			if (cameraDevices.Length > 0) {
				string deviceName = cameraDevices [0].name;

				camTexture = new WebCamTexture (deviceName, Screen.height/2, Screen.width/2, 60);
				img.canvasRenderer.SetTexture (camTexture);

				camTexture.Play ();
			} else {
				Debug.Log ("No video camera!");
			}

		}
	}




	void Update()
	{ 
		if (Input.gyro.enabled) {

			Quaternion target = Input.gyro.attitude;//new Quaternion (Input.gyro.attitude.x, Input.gyro.attitude.y, Input.gyro.attitude.z, Input.gyro.attitude.w)

			target = Quaternion.Euler (new Vector3( -target.eulerAngles.x,
				-target.eulerAngles.y, 
				target.eulerAngles.z));

			if (!_hasResetCharacterFacing) {
				CamRoot.transform.localRotation = target;
				resetCharacterFacing ();
				_hasResetCharacterFacing = true;
			}


			CamRoot.transform.localRotation = Quaternion.Slerp (CamRoot.transform.localRotation, target, Time.deltaTime * 720f);
		}



			//* Quaternion.FromToRotation(new Vector3(1,0,0), new Vector3(0,1,0));
//		Vector3 euler = target.eulerAngles;
//		euler.y = -euler.y;
//		target = Quaternion.Euler(euler);
		//Cube.transform.localRotation = Quaternion.Slerp (transform.localRotation, target, Time.deltaTime * 240f);


//		target = Quaternion.Euler (new Vector3( (target.eulerAngles.x>180?target.eulerAngles.x-360f:target.eulerAngles.x)+180f,
//			(target.eulerAngles.y>180?target.eulerAngles.y-360f:target.eulerAngles.y)+180f, 
//			(target.eulerAngles.z>180?target.eulerAngles.z-360f:target.eulerAngles.z)+180f));







		//Cube.transform.rotation = Quaternion.Euler (new Vector3 (0f, 0f, 0f));



	}

	void OnGUI()
	{
		return;


		GUI.skin.label.fontSize = 40;
//		GUI.Label ( new Rect(50f,100f, 1000f, 100f), "Label:" + Input.gyro.attitude.x
//			+ "/" + Input.gyro.attitude.y + "/" + Input.gyro.attitude.z );

		GUI.Label ( new Rect(50f,250f, 1000f, 100f), "Cam Local:" + CamRoot.transform.localRotation.eulerAngles.x
			+ "/" + CamRoot.transform.localRotation.eulerAngles.y + "/" + CamRoot.transform.localRotation.eulerAngles.z );

		GUI.Label ( new Rect(50f,400f, 1000f, 100f), "_floatCharacter_angle:" + _floatCharacter_angle.x
			+ "/" + _floatCharacter_angle.y + "/" + _floatCharacter_angle.z );

		GUI.Label ( new Rect(50f,550f, 1000f, 100f), "_hasResetCharacterFacing:" + _hasResetCharacterFacing.ToString());

		GUI.Label ( new Rect(50f,700f, 1000f, 100f), "_CamForward:" + _CamForward.x
			+ "/" + _CamForward.y + "/" + _CamForward.z );
		
		GUI.Label ( new Rect(50f,800f, 1000f, 100f), "gyro enabel:" + Input.gyro.enabled);
		GUI.Label ( new Rect(50f,900f, 1000f, 100f), "gyro device enabel:" + SystemInfo.supportsGyroscope);

	}


	bool _hasResetCharacterFacing = false;
	Vector3 _floatCharacter_angle= Vector3.zero;

	Vector3 _CamForward= Vector3.zero;

	void resetCharacterFacing()
	{
		_CamForward = Camera.main.transform.forward;

		Vector3 desiredCharacterForward = -_CamForward;

		CharacterRoot.transform.forward = desiredCharacterForward;

		Vector3 characterLocalEuler = CharacterRoot.transform.localRotation.eulerAngles;
		_floatCharacter_angle = characterLocalEuler;
		characterLocalEuler.x = 0f;
		characterLocalEuler.z = 0f;
		CharacterRoot.transform.localRotation = Quaternion.Euler (characterLocalEuler);

	}
}